﻿namespace DotNet.WinForm.Utilities.Controls
{
    public enum DotNetComboBoxStyle
    {
        DropDownList = 0
    }

    public class DotNetListItem
    {
        private string id = string.Empty;
        private string name = string.Empty;

        public DotNetListItem(string sid, string sname)
        {
            id = sid;
            name = sname;
        }

        public override string ToString()
        {
            return this.name;
        }

        public string ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }

    public class DotNetComboBox : DevExpress.XtraEditors.ComboBoxEdit
    {
        public DotNetComboBox()
        {
        }

        protected override void OnLoaded()
        {
            if (this.Properties.Buttons.Count == 0)
            {
                this.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            }
            base.OnLoaded();
        }

        private void DotNetComboBox_Paint()
        {
            if (_dataSource != null)
            {
                this.Items.Clear();
                var datas = _dataSource.ToTable();
                foreach (System.Data.DataRow row in datas.Rows)
                {
                    if (string.IsNullOrEmpty(this.DisplayMember) || string.IsNullOrEmpty(this.ValueMember))
                    {
                        this.Items.Add(row);
                    }
                    else
                    {
                        DotNetListItem item = new DotNetListItem(row[this.ValueMember].ToString(), row[this.DisplayMember].ToString());

                        this.Items.Add(item);
                    }
                }
            }
        }

        private DotNetComboBoxStyle _style = DotNetComboBoxStyle.DropDownList;

        public DotNetComboBoxStyle DropDownStyle
        {
            get
            {
                return _style;
            }
            set
            {
                _style = value;
            }
        }

        public DevExpress.XtraEditors.Controls.ComboBoxItemCollection Items
        {
            get
            {
                return this.Properties.Items;
            }
        }

        private string _DisplayMember = "";

        public string DisplayMember
        {
            get
            {
                return _DisplayMember;
            }
            set
            {
                _DisplayMember = value;
                DotNetComboBox_Paint();
            }
        }

        private string _ValueMember = "";

        public string ValueMember
        {
            get
            {
                return _ValueMember;
            }
            set
            {
                _ValueMember = value;

                DotNetComboBox_Paint();
            }
        }

        private System.Data.DataView _dataSource = null;

        public System.Data.DataView DataViewSource
        {
            get
            {
                return _dataSource;
            }
            set
            {
                this._dataSource = value;

                DotNetComboBox_Paint();
            }
        }

        public object SelectedValue
        {
            get
            {
                var selectV = this.SelectedItem as DotNetListItem;
                if (selectV != null)
                {
                    return selectV.ID;
                }
                else if (this.SelectedItem == null)
                {
                    return "";
                }
                else
                {
                    return this.SelectedItem;
                }
            }
            set
            {
                foreach (var item in this.Items)
                {
                    var si = item as DotNetListItem;
                    if (si != null && si.ID == value.ToString())
                    {
                        this.SelectedItem = item;
                        this.EditValue = item;
                        break;
                    }
                    else if (si == null && item.ToString() == value.ToString())
                    {
                        this.SelectedItem = item;
                        this.EditValue = item;
                        break;
                    }
                }
            }
        }

        public bool FormattingEnabled { get; set; }

        public int MaxDropDownItems { get; set; }
    }
}